package com.cens.mall.order.web;

import com.cens.mall.order.intercepter.LoginIntercepter;
import com.cens.mall.order.service.OrderService;
import com.cens.mall.order.vo.OrderConfirmVo;
import com.cens.mall.order.vo.OrderSubmitResponseVo;
import com.cens.mall.order.vo.OrderSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/toTrade")
    public String toTrade(Model model, HttpServletRequest request) throws ExecutionException, InterruptedException {
//        System.out.println(LoginIntercepter.loginUser.get());
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        model.addAttribute("orderConfirmData",orderConfirmVo);
        return "confirm";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo orderSubmitVo, Model model, RedirectAttributes redirectAttributes){
        OrderSubmitResponseVo vo = orderService.submitOrder(orderSubmitVo);
        if(vo.getCode() == 0){
            //下单成功来到支付选择页
            model.addAttribute("orderSubmitResponseVo",vo);
            return "pay";
        }else{
            String msg;
            switch (vo.getCode()){
                case 1:
                    msg="订单信息已过期，请重新提交";
                    break;
                case 2:
                    msg ="";
                    break;
                case 3:
                    msg ="";
                    break;
                default:
                    msg="未知异常";
                    break;
            }
            redirectAttributes.addFlashAttribute("msg",msg);
            return "redirect:http://order.censmall.com/toTrade";
        }
    }
}
